#include "EnthalpyError.h"
#include "CFDProblem.h"

template<>
InputParameters validParams<EnthalpyError>()
{
  InputParameters params = validParams<ElementIntegralPostprocessor>();
  params += validParams<MultiVariableInterface>();
  return params;
}

EnthalpyError::EnthalpyError(const InputParameters &parameters) :
	ElementIntegralPostprocessor(parameters),
	MultiVariableInterface(parameters),
	_cfd_problem(static_cast<CFDProblem&>(_fe_problem)),
	_cfd_data(_cfd_problem),
	_cfd_data_exact(_cfd_problem)
{
	// foreach(MooseVariable *var, _variables)
	for(auto var : _variables)
		addMooseVariableDependency(var);
}

Real EnthalpyError::getValue()
{
  return std::sqrt(ElementIntegralPostprocessor::getValue());
}

Real EnthalpyError::computeQpIntegral()
{
	Real mach = _cfd_problem._mach;
	Real gamma = _cfd_problem._gamma;
	Real pre = 1/gamma/mach/mach;
	Real density(1), velocity(1);
	Real rhoe = pre/(gamma-1) + 0.5*density*(velocity*velocity);
	Real h = (rhoe+pre)/density;

	for (int eq = 0; eq < _n_equation; ++eq)
	{
		_cfd_data.uh[eq] = (*_uh[eq])[_qp];
		_cfd_data.duh[eq] = (*_grad_uh[eq])[_qp];
	}
	_cfd_data.reinit();
	Real err =  _cfd_data.h-h;
	return err*err;
}
